package edu.gdkm.threaddemo;

import java.util.Random;
public class GiftThread extends Thread{
    //100个礼物
    static Integer count=100;

    public GiftThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        //同步锁定count,当一个线程使用count时，其他线程必须等待
        synchronized (count){
            while(count>0){
                System.out.println(this.getName()+"申请了一份礼物,剩余礼物数是："+--count);

                try {
                    //获取一个0-2000之前的随机整数，作为休眠的毫秒数，实现随机休眠
                    Thread.sleep(new Random().nextInt(2000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
